package tfsIssueTracker.commands;

import com.intellij.execution.configurations.GeneralCommandLine;
import tfsIssueTracker.SerializableIssueMention;
import tfsIssueTracker.Settings;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class FindIssueMentionsByModificationCommand extends TfsCommand<Collection<SerializableIssueMention>> {
    private final String modificationId;

    public FindIssueMentionsByModificationCommand(String modificationId, Settings settings) {
        super(settings);
        this.modificationId = modificationId;
    }

    @Override
    protected String getCommandName() {
        return "FindMentionsByChangeset";
    }

    @Override
    protected Collection<SerializableIssueMention> parseResult(String stdout) {
        List<SerializableIssueMention> result = new ArrayList<SerializableIssueMention>();
        String[] lines = stdout.split("\n");

        boolean firstLine = true;
        String id = null;

        for (String line: lines) {
            if (line.length() == 0) continue;

            if(firstLine) {
                id = line;
                firstLine = false;
            }
            else {
                result.add(new SerializableIssueMention(id, line));
                firstLine = true;
            }
        }
        return result;
    }

    @Override
    protected void addCommandSpecificArguments(GeneralCommandLine cli) {
        cli.addParameter(modificationId);
    }
}